#pragma once

#include <atlcrack.h>
#include <atlctrls.h>

#include <vector>
#include <string>

#include "atlcedlg.h"
#include "MemoEdit.h"

#include "resource.h"

#define DLG_FLAGS SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_DONEBUTTON

class EditDialog 
    : public CCEResizableDialog< EditDialog, DLG_FLAGS >
{
public:    

    EditDialog(const std::wstring &fName, bool unc) 
        : fileName(fName)
        , unicode(unc)
    {}

    typedef CCEResizableDialog< EditDialog, DLG_FLAGS > baseClass;
    enum { IDD = IDD_EDIT_DIALOG };

    BEGIN_MSG_MAP_EX(EditDialog)
        MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
        NOTIFY_CODE_HANDLER(GN_CONTEXTMENU, OnContextMenu)
        MSG_WM_SIZE(OnSize)
        MSG_WM_INITMENUPOPUP(OnInitMenuPopup)

        COMMAND_ID_HANDLER(ID_FILE_SAVE, OnSave)
        COMMAND_ID_HANDLER(ID_EDIT_UNDO, OnUndo)
        COMMAND_ID_HANDLER(ID_EDIT_REDO, OnRedo)
        COMMAND_ID_HANDLER(ID_EDIT_CUT, OnCut)
        COMMAND_ID_HANDLER(ID_EDIT_COPY, OnCopy)
        COMMAND_ID_HANDLER(ID_EDIT_PASTE, OnPaste)
        COMMAND_ID_HANDLER(ID_EDIT_SELECTALL, OnSelectAll)

        CHAIN_MSG_MAP(baseClass)
    END_MSG_MAP()

    bool OnClose();

    LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnContextMenu(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/);

    void OnInitMenuPopup(HMENU, UINT, BOOL);

    void OnSize(UINT, const CSize&);

    LRESULT OnSave(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
    LRESULT OnUndo(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) { memoCtrl.UndoEvent(); return FALSE; }
    LRESULT OnRedo(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) { memoCtrl.RedoEvent(); return FALSE; }
    LRESULT OnCut(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) { memoCtrl.Cut(); return FALSE; }
    LRESULT OnCopy(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) { memoCtrl.Copy(); return FALSE; }
    LRESULT OnPaste(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) { memoCtrl.Paste(); return FALSE; }
    LRESULT OnSelectAll(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) { memoCtrl.SelectAll(); return FALSE; }

protected:

    MemoEdit memoCtrl;
    
    const std::wstring fileName;
    const bool unicode;
};